#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL/SDL.h>
#endif

#include "math.h"
#include <iostream>
using namespace std;

class Bullet
{
public:
    int x_,y_,x2_,y2_,time,alpha;
    float angle, gangle, length; //must be a float or else numbers will round. Rounding = bad.
    bool m_active;
    void Init(int x, int y, float globalangle, float length)
    {
		gangle = globalangle;
        time=50;
        alpha=200;
        x_ = x;
        y_ = y;
        x2_ = 0;
        y2_ = 0;
		angle = gangle;
		if((rand()%10)>=4)
		{
			angle = angle + rand()%2;
		}
		else
		{
			angle = angle - rand()%2;
		}
        angle=(angle/180)*3.14;//convert to radians
        x2_ = length*cos(angle) +x_;
        y2_ = length*sin(angle) +y_;
        m_active = true;

    }

    bool Process()
    {


        if(time<0)

        {
            kill();

        }
        time--;
        if(alpha>0)
        {
        alpha-=4;
        }
		return true;
    }


    void kill()
    {
        m_active = false;
    }
private:
};


class BulletPool
{
public:
    static const int POOL_SIZE = 20;
    Bullet bullets_[POOL_SIZE];
    // void Create(int x, int y, int dir, int lifetime);

    void Process()
    {
        for (int i = 0; i < POOL_SIZE; i++)
        {
            if(bullets_[i].m_active)
            {
                bullets_[i].Process();
            }
        }
    }
    void Create(int x, int y, float angle, float length)
    {
        // find an available bullet
        for (int i = 0; i < POOL_SIZE; i++)
        {
            if (!bullets_[i].m_active)
            {
                bullets_[i].Init(x, y, angle,length);
                return;
            }
        }
    }
};



